package com.markedquilt.comment.controller;

import com.markedquilt.comment.coomon.BussinessException;
import com.markedquilt.comment.coomon.CommonRes;
import com.markedquilt.comment.coomon.EmBussinessError;
import com.markedquilt.comment.model.CategoryModel;
import com.markedquilt.comment.model.ShopModel;
import com.markedquilt.comment.service.CategoryService;
import com.markedquilt.comment.service.ShopService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.thymeleaf.util.StringUtils;

import java.io.IOException;
import java.math.BigDecimal;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author ZhangKe
 * @Description
 * @date 2020/4/21 15:03.
 */
@Controller("/shop")
@RequestMapping("/shop")
public class ShopController {

    @Autowired
    private ShopService shopService;

    @Autowired
    private CategoryService categoryService;

    //推荐服务v1.0
    @RequestMapping("/recommend")
    @ResponseBody
    public CommonRes recommend(@RequestParam(name = "longitude")BigDecimal longitude,
                               @RequestParam(name = "latitude")BigDecimal latitude) throws BussinessException {
        if (longitude==null || latitude==null){
            throw new BussinessException(EmBussinessError.PARAMETER_VALIDATION_ERROR);
        }
        List<ShopModel> shopModelList = shopService.recommend(longitude, latitude);
        return CommonRes.create(shopModelList);
    }

    //搜索服务v1.0
    @RequestMapping(value = "/search")
    @ResponseBody
    public CommonRes search(@RequestParam(name = "longitude")BigDecimal longitude,
                            @RequestParam(name = "latitude")BigDecimal latitude,
                            @RequestParam(name = "keyword")String keyword,
                            @RequestParam(name = "orderby",required = false)Integer orderby,
                            @RequestParam(name = "categoryId",required = false)Integer categoryId,
                            @RequestParam(name = "tags",required = false)String tags) throws BussinessException, IOException {
        if (StringUtils.isEmpty(keyword) || longitude==null || latitude==null){
            throw new BussinessException(EmBussinessError.PARAMETER_VALIDATION_ERROR);
        }

        Map<String,Object> result = shopService.searchES(longitude, latitude, keyword,orderby,categoryId,tags);
        List<ShopModel> shopModelList = (List<ShopModel>) result.get("shop");
        List<CategoryModel> categoryModelList = categoryService.selectAll();
        //List<Map<String,Object>> tagsAggregation = shopService.searchGroupByTags(keyword, categoryId, tags);
        List<Map<String,Object>> tagsAggregation = (List<Map<String, Object>>) result.get("tags");
        Map<String,Object> resMap=new HashMap<>();
        resMap.put("shop",shopModelList);
        resMap.put("category",categoryModelList);
        resMap.put("tags",tagsAggregation);

        return CommonRes.create(resMap);

    }
}
